<?php
/**
 * Created by PhpStorm.
 * User: gakuin
 * Date: 7/7/16
 * Time: 1:19 PM
 */
namespace app\models;

use Yii;
use yii\db\ActiveRecord;


class Proxy extends ActiveRecord
{

    public $proxy_id;
    public $user_id;
    public $proxy_grade;
    public $create_time;
    public $leader_id;

    public static function getsonrarray($userid){
      return  Proxy::find() -> where(['leader_id' => $userid,'del_flag'=>1]) -> asArray() -> all();
    }

    public static  function getProxyById($id){

        return Proxy::findOne(['user_id'=>$id,'del_flag'=>1]);
    }


    //获取代理列表表格数据
    public function getAgentListTableData($limit,$offset,$search,$leader_id){

        $total = (new \yii\db\Query())
            ->from('hcps_proxy')
            ->where(['leader_id' => $leader_id,'del_flag' => 1])
            ->count();
        $query=new \yii\db\Query();
        if (!empty($search)) {
            $rows = $query->where(['leader_id' => $leader_id,'del_flag' => 1])->andWhere(['or',['like', 'user_name', $search]])->from('hcps_proxy')->orderBy('create_time DESC')->limit($limit)->offset($offset)->all();
        }else{
            $rows = $query->where(['leader_id' => $leader_id,'del_flag' => 1])->from('hcps_proxy')->orderBy('create_time DESC')->limit($limit)->offset($offset)->all();
        }

        for($i=0;$i<count($rows);$i++){
            $p_arr= (new \yii\db\Query())->from('hcps_user')->where(['user_id' => $rows[$i]['user_id']])->one();
            $rows[$i]['user_name']=$p_arr['username'];
        }

        $rs=['total'=>$total,'rows'=>$rows];

        return $rs;

    }
}